/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.pbp.dao.impl;

import ac.pbp.core.db.DaoHelper;
import ac.pbp.core.exception.DataAdditionException;
import ac.pbp.core.exception.DataDeletionException;
import ac.pbp.core.exception.DataModificationException;
import ac.pbp.core.filter.FilterCriteria;
import ac.pbp.dao.PengarangDao;
import ac.pbp.dto.filter.PengarangFilterCriteria;
import ac.pbp.model.Pengarang;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Component;

/**
 *
 * @author Kim
 */
@Component
public class PengarangDaoImpl extends DaoHelper implements PengarangDao {

    @Override
    public List<Pengarang> loadAll(Long start, Long max) {
        return getSession().createQuery("FROM Pengarang ORDER BY kodePengarang ASC").setCacheable(true).list();
    }

    @Override
    public List<Pengarang> filter(FilterCriteria filterCriteria) {
        PengarangFilterCriteria filter = (PengarangFilterCriteria) filterCriteria;
        Criteria criteria = filterRoot(filter, getSession().createCriteria(Pengarang.class));
        long start = (filter.getPage() - 1) * filter.getMax();
        criteria.setFirstResult(Long.valueOf(start).intValue());
        criteria.setMaxResults(Long.valueOf(filter.getMax()).intValue());
        return criteria.setCacheable(true).list();
    }

    @Override
    public Long getMax(FilterCriteria filterCriteria) {
        PengarangFilterCriteria filter = (PengarangFilterCriteria) filterCriteria;
        Criteria criteria = filterRoot(filter, getSession().createCriteria(Pengarang.class));
        return new Integer(criteria.setCacheable(true).list().size()).longValue();
    }

    @Override
    public Long getMax() {
        return (Long)getSession().createQuery("SELECT COUNT(*) FROM Pengarang").setCacheable(true).uniqueResult();
    }

    @Override
    public void add(Pengarang bean) throws DataAdditionException {
        try {
            getHibernateTemplate().save(bean);
        } catch (Exception e) {
            throw new DataAdditionException("Add New Pengarang Failed!", e);
        }
    }

    @Override
    public Pengarang load(String kodePengarang) {
        return (Pengarang) getSession().load(Pengarang.class, kodePengarang);
    }

    @Override
    public List<Pengarang> loadAll() {
        return getSession().createQuery("FROM Pengarang ORDER BY kodePengarang ASC").setCacheable(true).list();
    }

    @Override
    public void update(Pengarang bean) throws DataModificationException {
        try {
            getHibernateTemplate().update(bean);
        } catch (Exception e) {
            throw new DataModificationException("Update Pengarang Failed!", e);
        }
    }

    @Override
    public void delete(Pengarang bean) throws DataDeletionException {
        try {
            getHibernateTemplate().delete(bean);
        } catch (Exception e) {
            throw new DataDeletionException("Delete Pengarang Failed!", e);
        }
    }
    
    public Criteria filterRoot(PengarangFilterCriteria filter, Criteria criteria) {
        criteria.addOrder(Order.asc("kodePengarang"));
        System.out.println("masuk filterRoot?");
                
        return criteria;
    }
}
